<?php
/**
 * Created by PhpStorm.
 * User: zoujianshuang
 * Date: 18-8-5
 * Time: 下午3:03
 */
namespace frontend\controllers;

use yii\web\Controller;
use Yii;
use common\controllers\HelpersTrait;

class WechatController extends Controller
{
    use HelpersTrait;

    public function actionIndex()
    {
        //token验证
        if (Yii::$app->request->isGet) {
            $token = Yii::$app->params['wechat']['token'];
            $signature = Yii::$app->request->get('signature');
            $timestamp = Yii::$app->request->get('timestamp');
            $nonce = Yii::$app->request->get('nonce');
            if (is_null($token) || is_null($signature) || is_null($timestamp) || is_null($nonce)) {
                return false;
            }
            // 将时间戳，随机字符串，token按照字母顺序排序并连接
            $tmp_arr = array($token, $timestamp, $nonce);
            sort($tmp_arr, SORT_STRING);// 字典顺序
            $tmp_str = implode($tmp_arr);//连接
            $tmp_str = sha1($tmp_str);// sha1签名   计算字符串的散列值

            if ($signature == $tmp_str) {
                return Yii::$app->request->get('echostr');
            } else {
                return false;
            }
        }
    }

    public function actionLogin()
    {
        return $this->render('login');
    }

    /**
     * 用户授权
     * @param $url 获取用户信息后跳转的地址
     */
    public function actionCallback()
    {
        $isWx = $this->fromWx();
//        if (!$isWx || Yii::$app->request->isAjax) {
//            throw new ForbiddenHttpException();
//        }
        $code = Yii::$app->request->get('code');
        echo $code; echo '<br/>';
        $state = Yii::$app->request->get('state');
        echo $state;die;
        $redirect = Yii::$app->request->get('redirect');
//        if (!Yii::$app->session->has('resourceOwnerId') || !Yii::$app->session->has('resourceOwnerNickName')) {
//            if ($code && $state == Yii::$app->session->get('getGrantState')) {
//                $wxClient = Yii::$container->get('wxClient');
//                $response = $wxClient->getGrant($code);
//                $info = $wxClient->getResourceOwnerInfo($response);
//                Yii::$app->session->set('resourceOwnerId', $response['resource_owner_id']);
//                Yii::$app->session->set('resourceOwnerNickName', $info['nickName']);
//            }
//
//            $resourceOwnerId = Yii::$app->session->get('resourceOwnerId');
//
//            if ($resourceOwnerId && Yii::$app->user->isGuest) {
//                $social = SocialConnect::findOne([
//                    'resourceOwner_id' => $resourceOwnerId,
//                    'provider_type' => SocialConnect::PROVIDER_TYPE_WECHAT,
//                ]);
//
//                if (!is_null($social) && $social->isAutoLogin) {
//                    $user = User::findOne($social->user_id);
//                    if (!is_null($user)) {
//                        Yii::$app->user->login($user);    //微信绑定,自动登录
//                        $redis = Yii::$app->redis;
//                        $equipment = CLIENT_TYPE == 'pc' ? 'pc' : 'wap';
//                        $loginSign = Yii::$app->session->getId();
//                        if (!empty($equipment) && !empty($loginSign) && !empty($user)) {
//                            //当前用户是否存在登录状态   array
//                            $redisContent = json_decode($redis->hget('login_status_user', $user->id), true);
//                            $redisContent[$equipment] = $loginSign;
//                            $redis->hset('login_status_user', $user->id, json_encode($redisContent));
//                        }
//                    }
//                }
//            }
//        }

        return Yii::$app->controller->redirect($redirect);
    }
}